package com.wyx.controller.system;


import com.wyx.aspectj.annotation.Log;
import com.wyx.aspectj.enums.BusinessType;
import com.wyx.domain.DictType;
import com.wyx.dto.DictTypeDto;
import com.wyx.service.DictTypeService;
import com.wyx.utils.ShiroSecurityUtils;
import com.wyx.vo.AjaxResult;
import com.wyx.vo.DataGridView;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * @author xiaoxiong
 * @version 1.0
 * @date 2021/2/26 12:27
 */
@RestController
@Log4j2
@RequestMapping("system/dict/type")
public class DictTypeController {

    @Autowired
    private DictTypeService dictTypeService;

    /**
     * 分页查询
     *
     * @return
     */
    @GetMapping("listForPage")
    public AjaxResult listForPage(DictTypeDto dictTypeDto) {
        DataGridView dataGridView = this.dictTypeService.listPage(dictTypeDto);
        return AjaxResult.success("查询成功", dataGridView.getData(), dataGridView.getTotal());
    }

    /**
     * 添加
     *
     * @return
     */
    @PostMapping("addDictType")
    @Log(title = "添加字典数据类型", businessType = BusinessType.INSERT)
    public AjaxResult addDictType(@Validated DictTypeDto dictTypeDto) {
        if (dictTypeService.checkDictTypeUnique(dictTypeDto.getDictId(), dictTypeDto.getDictType())) {
            return AjaxResult.fail("新增字典失败[" + dictTypeDto.getDictName() + "]" + "存在");
        }
        dictTypeDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(this.dictTypeService.insert(dictTypeDto));
    }

    /**
     * 更新操作
     *
     * @param dictTypeDto
     * @return
     */
    @PutMapping("updateDictType")
    @Log(title = "更新字典数据类型", businessType = BusinessType.UPDATE)
    public AjaxResult updateDictType(@Validated DictTypeDto dictTypeDto) {
        if (dictTypeService.checkDictTypeUnique(dictTypeDto.getDictId(), dictTypeDto.getDictType())) {
            return AjaxResult.fail("更新字典失败[" + dictTypeDto.getDictName() + "]" + "字典不存在");
        }
        dictTypeDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(this.dictTypeService.update(dictTypeDto));
    }

    /**
     * 根据dictId 查询字典
     *
     * @param dictId
     * @return
     */
    @GetMapping("getOne/{dictId}")
    public AjaxResult getDictyTypeById(@PathVariable @Validated @NotNull(message = "dictId 不能为空") Long dictId) {
        if (dictId == null) {
            return AjaxResult.fail(dictId + "不能为空");
        }
        DictType dictType = this.dictTypeService.selectDictTypeById(dictId);
        if (dictType == null) {
            return AjaxResult.fail("dictId 不存在");
        }
        return AjaxResult.success(dictType);
    }

    /**
     * 根据id 删除DictType
     *
     * @return
     */
    @DeleteMapping("deleteDictTypeByIds/{dictIds}")
    @Log(title = "删除字典数据类型", businessType = BusinessType.DELETE)
    public AjaxResult delectDictType(@PathVariable @Validated @NotEmpty(message = "dictIds不能为空") Long[] dictIds) {
        if (dictIds == null) {
            return AjaxResult.fail(dictIds + "不能为空");
        }
        return AjaxResult.toAjax(this.dictTypeService.deleteDictTypeByIds(dictIds));
    }


    /**
     * 查询所有可用的字典类型
     *
     * @return
     */
    @GetMapping("selectAllDictType")
    public AjaxResult selectAllDictType() {
        return AjaxResult.success(this.dictTypeService.list().getData());
    }


    /**
     * 字典缓存同步
     *
     * @return
     */
    @GetMapping("/dictCacheAsync")
    @Log(title = "字典缓存同步", businessType = BusinessType.OTHER)
    public AjaxResult dictCacheAsync() {
        try {
            this.dictTypeService.dictCacheAsync();
            return AjaxResult.success();
        } catch (Exception e) {
            log.error("字典缓存同步失败：", e);
            return AjaxResult.fail("字典缓存同步失败");
        }
    }


}
